
$(function () {
    let form = layui.form
    let layer = layui.layer
    //点击注册账号按钮
    $('#reg').on('click', function () {
        $('.login-box').hide();
        $('.reg-box').show();
    })

    //点击登录账号按钮
    $('#login').on('click', function () {
        $('.login-box').show();
        $('.reg-box').hide();
    })

    $('#regSubmit').on('submit', function (e) {
        e.preventDefault()
        console.log(111);
        $.ajax({
            type: "post",
            url: "/api/reguser",
            // data: {
            //     username: $('#uname').val(),
            //     password: $('#pwd').val(),
            // },
            data: $(this).serialize(),
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg('注册失败')
                }
                $('#login').click()
            }
        });
    })

    $('#loginSubmit').on('submit', function (e) {
        e.preventDefault()
        $.ajax({
            type: "post",
            url: "/api/login",
            data: $(this).serialize(),
            success: function (res) {
                console.log(res.status);
                if (res.status == 0) {
                    //把服务器返回的token值存到本地,已证明已经登录过
                    localStorage.setItem('token', res.token)
                    //跳转到主页面
                    location.href = '/index.html'
                }
            }
        });
    })

    form.verify({
        samepwd: function (val) {
            if (val !== $('#pwd').val()) {
                //校验失败,返回错误提示消息
                return '两次密码不一致'
            }
        },
        pass: [
            /^[\S]{6,12}$/
            , '密码必须6到12位，且不能出现空格'
        ]
    })


})